// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <benchmark/benchmark.h>

#include <nebula/compute/api_vector.h>
#include <tests/compute/kernels/test_util.h>
#include <nebula/testing/ktest_util.h>
#include <nebula/testing/random.h>
#include <nebula/util/benchmark_util.h>

namespace nebula {
namespace compute {
constexpr auto kSeed = 0x0ff1ce;

static void NthToIndicesBenchmark(benchmark::State& state,
                                  const std::shared_ptr<Array>& values, int64_t n) {
  for (auto _ : state) {
    ABORT_NOT_OK(NthToIndices(*values, n).status());
  }
  state.SetItemsProcessed(state.iterations() * values->length());
}

static void NthToIndicesInt64(benchmark::State& state) {
  RegressionArgs args(state);

  const int64_t array_size = args.size / sizeof(int64_t);
  auto rand = random::RandomArrayGenerator(kSeed);

  auto min = std::numeric_limits<int64_t>::min();
  auto max = std::numeric_limits<int64_t>::max();
  auto values = rand.Int64(array_size, min, max, args.null_proportion);

  NthToIndicesBenchmark(state, values, array_size / 2);
}

BENCHMARK(NthToIndicesInt64)
    ->Apply(RegressionSetArgs)
    ->Args({1 << 20, 100})
    ->Args({1 << 23, 100})
    ->Unit(benchmark::TimeUnit::kNanosecond);

}  // namespace compute
}  // namespace nebula
